package com.yehui.alibaba.service.at.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.yehui.alibaba.dao.OrderDAO;
import com.yehui.alibaba.entity.OrderEntity;
import com.yehui.alibaba.service.at.OrderService;
import io.seata.rm.tcc.api.BusinessActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author 业徽
 * @date 2021/8/4
 */
@Service("orderService")
public class OrderServiceImpl implements OrderService {

    private static final Logger logger = LoggerFactory.getLogger(OrderServiceImpl.class);

    @Resource
    private OrderDAO orderDao;

    /**
     * 入库
     *
     * @param actionContext
     * @param userId
     * @param productId
     * @param amount
     * @return
     */
    @Override
    @DS(value = "order-ds")
    @Transactional(propagation = Propagation.REQUIRES_NEW) // 开启新事物
    public Integer add(BusinessActionContext actionContext, Long userId, Long productId, Integer amount) {
        // 保存订单
        OrderEntity order = new OrderEntity().setUserId(userId).setProductId(productId).setPayAmount(amount);
        orderDao.saveOrder(order);
        logger.info("[createOrder] 保存订单: {}", order.getId());
        // 返回订单编号
        return order.getId();
    }
}
